import logging

import atom
import scheduler

log = logging.getLogger('sched')

class TypesConfig:
    def __init__(self, atom_store):
        t = atom.AtomType('test')
        t.add_table('test', ('SELECT guid FROM test WHERE i = ?', 'i'))
        atom_store.reg_type(t)

class Scheduler(scheduler.Scheduler):
    def __init__(self, sync, send_fcn):
        scheduler.Scheduler.__init__(self, send_fcn)
        self.sync_ = sync

        # Peers that has active queues in the system
        self.peers_ = []
        
    def activate_queues(self, c_id):
        if c_id.peer_id in self.peers_:
            return
        q = scheduler.VerSetDiff_Queue(c_id.peer_id,
                                       self.sync_.get_own_vs(),
                                       self.sync_.get_peer_vs(c_id.peer_id))
        self.add_queue(q)
        self.peers_.append(c_id.peer_id)

        s = scheduler.Scheme(c_id.peer_id, [c_id.peer_id])
        self.add_scheme(s)

    def update(self, msg):
        for q in self.queues_:
            self.queues_[q].append(msg)

    def classify(self, msg, label):
        # Note: the version is already implicitly added to the VerSetDiff_Queues
        return []
        
    def match_scheme(self, c_id):
        assert c_id.peer_id in self.peers_
        return c_id.peer_id
